package cn.itlight.product.service.impl;

import cn.itlight.basic.query.BaseQuery;
import cn.itlight.basic.utils.BaiduAuditUtils;
import cn.itlight.basic.utils.PageList;
import cn.itlight.pet.domain.Pet;
import cn.itlight.pet.domain.PetDetail;
import cn.itlight.pet.domain.PetOnlineAuditLog;
import cn.itlight.product.domain.Product;
import cn.itlight.product.domain.ProductDetail;
import cn.itlight.product.domain.ProductOnlineAuditLog;
import cn.itlight.product.mapper.ProductDetailMapper;
import cn.itlight.product.mapper.ProductMapper;
import cn.itlight.product.mapper.ProductOnlineAuditLogMapper;
import cn.itlight.product.service.IProductService;
import cn.itlight.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tomliu
 * @since 2022-07-21
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Override
    public void save(Product product) {
        // 1.保存宠物
        productMapper.save(product);
        // 2.设置宠物详情关联宠物
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        // 3.保存宠物详情
        productDetailMapper.save(detail);
    }


    @Override
    public void update(Product product) {
        productMapper.update(product);
        // 2.设置宠物详情关联宠物
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        if(detail.getId() == null){
            productDetailMapper.save(detail);
        } else {
            productDetailMapper.update(detail);
        }
    }

    @Override
    public void remove(Long id) {

        // 1.删除宠物详情
        productDetailMapper.removeByProductId(id);
        productMapper.remove(id);
    }

    /*
    * @Title: onsale
    * @Description: 服务上架
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/21 10:14
    * @Parameters:
    * @Return
    */
    @Override
    public void onsale(List<Long> ids) {
        for (Long id : ids){
            // 1.得到每个宠物基本信息对象
            Product product = productMapper.loadById(id);
            StringBuffer sbf = new StringBuffer();
            // 2.调用百度AI进行文字，图片的审核
            if (!BaiduAuditUtils.TextCensor(product.getName())){
                sbf.append("名称不合法，名称为：" + product.getName());
            }
            ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
            productOnlineAuditLog.setProductId(id);
            if (sbf.length() > 0){
                // 2.1.如果违规，我们要保存审核失败日志

                productOnlineAuditLog.setNote(sbf.toString());
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
                continue;
            }
            productOnlineAuditLog.setNote("审核成功！");
            productOnlineAuditLogMapper.save(productOnlineAuditLog);
            productMapper.onsale(id);
        }
    }

    /*
    * @Title: offsale
    * @Description: 服务下架
    * @Author: TomLiu
    * @Version: 1.0
    * @Date:  2022/7/21 10:14
    * @Parameters:
    * @Return
    */
    @Override
    public void offsale(List<Long> ids) {
        productMapper.offsale(ids);
    }

    @Override
    public void batchDel(Long[] ids) {
        productDetailMapper.batchDelByPetId(ids);
        productMapper.batchDel(ids);
    }
}
